package org.supper.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.supper.auth.entity.Auth;
import org.supper.auth.entity.UserEntity;
import org.supper.auth.service.LoginService;


import javax.annotation.Resource;

@Service
public class UserDetailService implements UserDetailsService {
    @Resource
    private LoginService loginService;
    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        LambdaQueryWrapper<UserEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserEntity::getUsername,userName);
        queryWrapper.eq(UserEntity::getDataActive,1);
        UserEntity userEntity = loginService.getOne(queryWrapper);
        Auth auth = new Auth();
        auth.setUsername(userEntity.getUsername());
        auth.setId(userEntity.getId());
        auth.setHandeUrl(userEntity.getHandeUrl());
        auth.setPassword(userEntity.getPassword());
        auth.setAuthorities( AuthorityUtils.createAuthorityList("ADMIN1"));
        return auth;
    }

}
